package com.dt;

import com.dt.common.utils.StringUtils;
import com.dt.common.utils.websocket.WebSocket;
import com.dt.system.domain.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.dt.system.service.ITaskService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务的使用
 * @author yxg
 **/
@Component
public class RemindTask
{
    @Autowired
    private ITaskService taskService;
    //上午6点到下午6点 每5分钟执行一次   格式：[秒] [分] [小时] [日] [月] [周] [年]  https://cron.ciding.cc/
    //@Scheduled(cron="0 0/5 6,18 * * ? ")
    @Scheduled(cron="0/5 * * * * ? ")
    public void execute(){

        //获取所有在线的app
        Map<String, WebSocket> clients = WebSocket.getClients();

        for(String key: clients.keySet()) {
            if(StringUtils.isEmpty(key)){
                continue;
            }
            //根据用户id。获取超时的任务列表
            List<Task> tasks = taskService.selectRemindTaskListByUid(Long.parseLong(key));
            try {
                if(!tasks.isEmpty())
                {
                    clients.get(key).sendMessageTo("你有"+tasks.size()+"条任务未完成，请及时完成",key);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}